import axios from "axios"
import { Message } from "element-ui"
import router from "./router"

axios.interceptors.request.use(
    config => {
        let token = localStorage.token
        if (token) {
            config.headers.authorization = token
        }
        return config
    },
    error => {
        return Promise.reject(error)
    }
)

axios.interceptors.response.use(
    response => {
        return response
    },
    error => {
        let code = error.response.status 
        if (code == 401 || code == 403) {
            localStorage.removeItem("token")
            router.push("/login")
            Message.error("请重新登录")
        } else {
            return Promise.reject(error)
        }
    }
)

export default axios